package com.msg2phone.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class SimpleUrlApp {
	
	//http://
	public static final String HTTP_PROTOCAL = "http://";
	
	public static String getUrlData(String domain,String recource,NetType netType) {
		
		StringBuffer document = new StringBuffer();
		try {
			HttpURLConnection connection = getConnByType(domain,recource ,netType);
			
			connection.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			String line = null;
			while ((line = reader.readLine()) != null)
				document.append(line);
			Log.e("ddd", document.toString());
			return document.toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
	
	public static HttpURLConnection getConnByType(String domain,String recource, NetType netType) throws IOException {
		URL urlget;
		HttpURLConnection connection;
			
		if (netType != null && netType.isWap()) {
			urlget =  new  URL("http://10.0.0.172:80"+recource);
			connection = (HttpURLConnection)urlget.openConnection();  
		    connection.setRequestProperty("X-Online-Host",domain);
		    connection.setRequestProperty("Accept","*/*");
		}
		else{
			urlget = new URL(HTTP_PROTOCAL + domain + recource);
			connection = (HttpURLConnection) urlget.openConnection();
			connection.setDoOutput(true);
			connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
		}
		
		return connection;
	}
}
